package com.ruoyi.base.service.impl;

import com.ruoyi.base.domain.Build;
import com.ruoyi.base.domain.Floor;
import com.ruoyi.base.domain.Room;
import com.ruoyi.base.domain.SchoolArea;
import com.ruoyi.base.domain.TreeNode;
import com.ruoyi.base.mapper.RoomMapper;
import com.ruoyi.base.service.FloorService;
import com.ruoyi.base.service.IFloorBuildService;
import com.ruoyi.base.service.IRoomService;
import com.ruoyi.base.service.ISchoolAreaService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.data.domain.TdataHtEq;
import com.ruoyi.data.domain.TdataRtRs;
import com.ruoyi.data.service.ITdataHtEqService;
import com.ruoyi.data.service.ITdataHtWgService;
import com.ruoyi.data.service.ITdataRtRsService;
import com.ruoyi.data.service.ITdataRtWgcService;
import com.ruoyi.iot.domain.CjqMeter;
import com.ruoyi.param.domain.TparamEq;
import com.ruoyi.param.domain.TparamTm;
import com.ruoyi.param.domain.TparamTp;
import com.ruoyi.param.domain.TparamUe;
import com.ruoyi.param.service.ITparamEqService;
import com.ruoyi.param.service.ITparamTmService;
import com.ruoyi.param.service.ITparamTpService;
import com.ruoyi.param.service.ITparamUeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 房间档案Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-03
 */
@Service
public class RoomServiceImpl implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private IFloorBuildService floorBuildService;
    @Autowired
    private FloorService floorService;
    @Autowired
    private ISchoolAreaService schoolAreaService;
    @Autowired
    private ITdataHtEqService htEqService;
    @Autowired
    private ITdataHtWgService htWgService;
    @Autowired
    private ITdataRtRsService rtRsService;
    @Autowired
    private ITdataRtWgcService rtWgcService;
    @Autowired
    private ITparamEqService tparamEqService;
    @Autowired
    private ITparamTmService tparamTmService;
    @Autowired
    private ITparamTpService tparamTpService;
    @Autowired
    private ITparamUeService tparamUeService;

    /**
     * 查询房间档案
     *
     * @param id 房间档案主键
     * @return 房间档案
     */
    @Override
    public Room selectRoomById(String id) {
        return roomMapper.selectRoomById(id);
    }

    /**
     * 查询房间档案列表
     *
     * @param room 房间档案
     * @return 房间档案
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间档案
     *
     * @param room 房间档案
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        room.setId(UUID.randomUUID().toString());

        htEqService.insertSelective(new TdataHtEq().setId(UUID.randomUUID().toString()).setRoomId(room.getId()).setDt(LocalDateTime.now())
                .setRunStatus(1L));

//        htWgService.insertSelective(new TdataHtWg().setId(UUID.randomUUID().toString()).setRoomId(room.getId()).setDt(LocalDateTime.now())
//                .setRunStatus(1L));

        rtRsService.insertSelective(new TdataRtRs().setId(UUID.randomUUID().toString()).setRoomId(room.getId()).setLoopNo(1).setRunStatus(1).setWorkStatus(0)
                .setStopReason(0).setVol(BigDecimal.ZERO).setCur(BigDecimal.ZERO).setYggl(BigDecimal.ZERO).setEdgl(BigDecimal.ZERO).setLoopEq(BigDecimal.ZERO)
                .setDt(LocalDateTime.now()));

//        rtWgcService.insertSelective(new TdataRtWgc().setId(UUID.randomUUID().toString()).setRoomId(room.getId()).setDt(LocalDateTime.now()).setRunStatus(1));

        tparamEqService.insertSelective(new TparamEq().setId(UUID.randomUUID().toString()).setRoomId(room.getId()).setRunStatus(1));

        tparamTmService.insertSelective(new TparamTm().setId(UUID.randomUUID().toString()).setRoomId(room.getId()).setRunStatus(1));

        tparamTpService.insertSelective(new TparamTp().setId(UUID.randomUUID().toString()).setRoomId(room.getId()).setRunStatus(1).setLoopNo(1).setTpNo(1));

        tparamUeService.insertSelective(new TparamUe().setId(UUID.randomUUID().toString()).setRoomId(room.getId()).setRunStatus(1).setLoopNo(1));

        return roomMapper.insertRoom(room);
    }

    /**
     * 修改房间档案
     *
     * @param room 房间档案
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        return roomMapper.updateRoom(room);
    }

    /**
     * 修改房间档案
     *
     * @param rooms 房间档案
     * @return 结果
     */
    @Override
    public int batchUpdateRoom(List<Room> rooms) {
        int num = 0;
        for (Room room : rooms) {
            num += roomMapper.updateRoom(room);
        }
        return num;
    }

    /**
     * 批量删除房间档案
     *
     * @param ids 需要删除的房间档案主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(String[] ids) {
        htEqService.deleteByRoomIds(Arrays.asList(ids));
//        htWgService.deleteByRoomIds(Arrays.asList(ids));
        rtRsService.deleteByRoomIds(Arrays.asList(ids));
//        rtWgcService.deleteByRoomIds(Arrays.asList(ids));
        tparamEqService.deleteByRoomIds(Arrays.asList(ids));
        tparamTmService.deleteByRoomIds(Arrays.asList(ids));
        tparamTpService.deleteByRoomIds(Arrays.asList(ids));
        tparamUeService.deleteByRoomIds(Arrays.asList(ids));
        return roomMapper.deleteRoomByIds(ids);
    }

    /**
     * 删除房间档案信息
     *
     * @param id 房间档案主键
     * @return 结果
     */
    @Override
    public int deleteRoomById(String id) {
        return roomMapper.deleteRoomById(id);
    }

    @Override
    public CjqMeter selectCjqMeter(String roomId) {
        return roomMapper.selectCjqMeter(roomId);
    }

    @Override
    public List<TreeNode> getRoomTree(String name, String id) {
        if (name == null) {
            name = "";
        }
        switch (name) {
            case "build": {
                return floorBuildService.selectFloorBuildList(new Build().setPId2(id))
                        .stream().map(b -> new TreeNode<>().setId(b.getId()).setName(b.getName()).setModel(b).setParentId(id).setLeaf(false))
                        .collect(Collectors.toList());
            }
            case "floor": {
                return floorService.floorListAll(new Floor().setBuildId(id))
                        .stream().map(f -> new TreeNode<>().setId(f.getId()).setName(f.getFloorNoName()).setModel(f).setParentId(id).setLeaf(false))
                        .collect(Collectors.toList());
            }
            case "room": {
                return this.selectRoomList(new Room().setFloor(id))
                        .stream().map(r -> new TreeNode<>().setId(r.getId()).setName(r.getRoomNo()).setModel(r).setParentId(id).setLeaf(true))
                        .collect(Collectors.toList());
            }
            default: {
                return schoolAreaService.selectSchoolAreaList(new SchoolArea())
                        .stream().map(a -> new TreeNode().setId(a.getId()).setName(a.getName()).setModel(a).setParentId(id).setLeaf(false))
                        .collect(Collectors.toList());
            }
        }
    }

    @Override
    public List<CjqMeter> selectCjqMeterList(List<String> roomIds) {
        return roomMapper.selectCjqMeterList(roomIds);
    }

    @Override

    public int batchPrice(List<String> roomIds, BigDecimal eqPrice) {
        if (roomIds == null || roomIds.size() == 0) {
            throw new ServiceException("roomIds不可为空");
        }
        if (eqPrice == null) {
            throw new ServiceException("eqPrice不可为空");
        }
        return roomMapper.batchPrice(roomIds, eqPrice);
    }

    @Override
    public int deviceSet(Room room) {
        if (room == null) {
            throw new ServiceException("body为空");
        }
        if (room.getId() == null || room.getId().equals("")) {
            throw new ServiceException("id不可为空");
        }
        if (room.getMeterNo() == null || room.getMeterNo().equals("")) {
            throw new ServiceException("meterNo不可为空");
        }
        return roomMapper.deviceSet(room);
    }

    @Override
    public List<String> selectAllRoomId() {
        return roomMapper.selectAllRoomId();
    }

    @Override
    public List<String> selectAllNewRoomId() {
        return roomMapper.selectAllNewRoomId();
    }

    @Override
    public List<String> selectAllOldRoomId() {
        return roomMapper.selectAllOldRoomId();
    }
}
